package com.wangbo.format;

import java.text.DecimalFormat;
import java.util.Date;

public class TestStringFormat {
    public static void main(String[] args) {

    }

    /**
     * Date -> String (yyyy-MM-dd)
     */
    private static void test1() {
        String date = String.format("%tF", new Date(System.currentTimeMillis() - (long) 24 * 3600 * 1000));
        System.out.println(date);  // 2020-04-18
    }

    /**
     * Date -> String (yyyy-MM-dd HH:mm:ss)
     */
    private static void test2(){
        String date = String.format("%tF %<tT",new Date());
        System.out.println(date);  // 2020-04-19 18:31:09
    }

    /**
     * double -> String 保留两位小数
     */
    private static void test3() {
        double sqrt = Math.sqrt(10);
        System.out.println(String.format("%.2f",sqrt));  // 3.16
    }
}
